package com.quanthium;


import com.alibaba.fastjson.annotation.JSONField;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;

import java.util.HashMap;
import java.util.Map;


@Data
@Document(collection = "device_shadow")
@AllArgsConstructor
public class DeviceShadow {

//    @Id
//    private String id;
    @Field("product_id")
    private String productId;
    @Field("device_id")
    private String deviceId;
    @Field
    private State state;
    @Field
    private Metadata metadata;
    @Field
    private String version = "1.0";
    @Field
    private Long timestamp = System.currentTimeMillis();


    public DeviceShadow() {
        this.state = new State();
        this.metadata = new Metadata();
    }



    @Data
    public static class State {
        @JSONField(name = "reported")
        private Map<String, Object> reported = new HashMap<>();

        @JSONField(name = "desired")
        private Map<String, Object> desired = new HashMap<>();
    }

    @Data
    public static class Metadata {
        @JSONField(name = "reported")
        private Map<String, Object> reported = new HashMap<>();

        @JSONField(name = "desired")
        private Map<String, Object> desired = new HashMap<>();
    }

    // 添加上报属性
    public void addStateReported(String key, Object value) {
        this.state.reported.put(key, value);
    }

    // 添加期望属性
    public void addStateDesired(String key, Object value) {
        this.state.desired.put(key, value);
    }

    // 添加上报属性
    public void addMetadataReported(String key) {
        Map<String, Object> timestamp = new HashMap<>();
        timestamp.put("timestamp", System.currentTimeMillis());
        this.metadata.reported.put(key, timestamp);
    }

    // 添加期望属性
    public void addMetadataDesired(String key, Object value) {
        Map<String, Object> timestamp = new HashMap<>();
        timestamp.put("timestamp", System.currentTimeMillis());
        this.metadata.desired.put(key, timestamp);
    }
}